package com.ruoyi.web.job;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.TblInterfaces;
import com.ruoyi.system.mapper.TblInterfacesMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Configuration
@EnableScheduling
public class FiveMinutesCheckTask {
    private static final String FS_NOTIFY_MSG = "https://sany-one-prod.sany.com.cn/messagesender/fs/v1/sendFsTxtMsg";
	private static Logger logger = LogManager.getLogger(FiveMinutesCheckTask.class);

	@Autowired
	private TblInterfacesMapper tblInterfacesMapper;

	//秒 分 时 日 月 周 年
	@Scheduled(cron="0 */5 * * * ?")
    public void taskEndFissionWanted(){
    	//每天晚上0点执行
        //0 */5 * * * ?
        logger.info("5分钟定时检查所有接口... ");
		List<TblInterfaces> tblInterfacesList = tblInterfacesMapper.selectTblInterfacesList(null);
		if (tblInterfacesList == null || tblInterfacesList.isEmpty()){
		    logger.info("5分钟检查接口没有接口数据，返回");
		    return;
        }
        for (TblInterfaces item:tblInterfacesList) {
		    if (item == null){
                logger.info("对象内容为空，跳过");
		        continue;
            }
            if (item.getMethod() == null){
                logger.info("请求方法为空，跳过");
		        continue;
            }
            if (StringUtils.isEmpty(item.getSuccessStr())){
                logger.info("没有成功字符串，跳过");
                continue;
            }

            logger.info("开始请求[{}]-[{}]", item.getUrl(), item.getProject());
            if (StringUtils.equals(item.getMethod().toLowerCase(), "get")){
                StringBuilder sb = new StringBuilder(item.getUrl());
                if (StringUtils.isNotEmpty(item.getUrlParams())){
                    sb.append(item.getUrlParams());
                }
                String result = HttpUtils.sendGet(sb.toString());
                if (StringUtils.isEmpty(result) || !result.contains(item.getSuccessStr())){
                    //失败了，通知要监控的对象。
                    sendFsMsg(item, result);
                }
            }
        }
        System.out.println(tblInterfacesList);
    }

    public static void sendFsMsg(TblInterfaces item, String result){
        logger.info("开始通知失败信息[{}]", item);
        FsMsgSendRequestModel fsMsgModel = new FsMsgSendRequestModel();
        fsMsgModel.setAppId("cli_a1597e58c1f8d076");
        fsMsgModel.setAppSecret("rs5F2msjsUrRZVkIBNbpxhLMUKDUEyUd");

        String[] fsUserNames = item.getFsNotifyNames().split(",");
        for (String userName:fsUserNames) {
            fsMsgModel.setUserName(userName);
            fsMsgModel.setContent("项目: " + item.getProject() + "\n"
                    + "接口: " + item.getUrl()
                    + "请求失败\n"
                    + "返回内容: " + result);

            try {
                HttpUtils.testPost(FS_NOTIFY_MSG, fsMsgModel);
            } catch (Exception e) {
                logger.error("", e);
            }
        }
    }

    public static void main(String[] args){
        TblInterfaces item = new TblInterfaces();
        item.setFsNotifyNames("huangrw2");
        sendFsMsg(item, "https://baid..com");
    }

}